﻿using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using ECommerce.Library.DataStructure;

namespace AutoGear.BLL
{
    public class AddressBook
    {
        #region AddressBook Private Fields


        int _addressBookId;
        int _userId;
        string _userFirstName = string.Empty;
        string _userLastName = string.Empty;
        string _userTelephone1 = string.Empty;
        string _userTelephone2 = string.Empty;
        string _userEmailAddress = string.Empty;
        string _userAddressLine1 = string.Empty;
        string _userAddressLine2 = string.Empty;
        string _companyName = string.Empty;
        string _userCity = string.Empty;
        string _userState = string.Empty;
        string _userZip = string.Empty;
        string _userCountry = string.Empty;
        bool _isDefault;

        #endregion
        #region AddressBook Constractor
		/// <summary>
        /// Creates an empty object of TblAddressBook. 
        /// This is mainly used to create a new instance of TblAddressBook to add a new TblAddressBook.
        /// </summary>
		public AddressBook(){}
		
				
		/// <summary>
        /// Used to retrieve employee information from database, 
        /// which is also used by Update method to save information of an existing TblAddressBook.
        /// </summary>
		public AddressBook
		(
			int addressBookId,
			int userId,
			string userFirstName,
			string userLastName,
			string userTelephone1,
			string userTelephone2,
			string userEmailAddress,
			string userAddressLine1,
			string userAddressLine2,
            string companyName,
			string userCity,
			string userState,
			string userZip,
			string userCountry,
			bool isDefault
		)
		{
			_addressBookId = addressBookId;	
			_userId = userId;	
			_userFirstName = userFirstName;	
			_userLastName = userLastName;	
			_userTelephone1 = userTelephone1;	
			_userTelephone2 = userTelephone2;	
			_userEmailAddress = userEmailAddress;	
			_userAddressLine1 = userAddressLine1;	
			_userAddressLine2 = userAddressLine2;
            _companyName = companyName;
			_userCity = userCity;	
			_userState = userState;	
			_userZip = userZip;	
			_userCountry = userCountry;	
			_isDefault = isDefault;	
		}
		
		#endregion
        #region AddressBook Properties

        /***********************************************************************************************
                                    -- PROPERTIES --        
         Contains all the properties representing the data fields of the current entity.
         ***********************************************************************************************/

        /// <summary>
        /// This is the Primary key value of the AddressBookId, which can't be assigned from the application,
        /// to ensure the data consistancy, by not letting the user to change the PK_TblAddressBook to update the data of an another TblAddressBook instance.
        /// For view or updation purpose, every TblAddressBook will be retrieved thru the proper 'Get' method.
        /// </summary>

        public int AddressBookId
        {
            get { return _addressBookId; }
        }


        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        public string UserFirstName
        {
            get { return _userFirstName; }
            set { _userFirstName = value; }
        }

        public string UserLastName
        {
            get { return _userLastName; }
            set { _userLastName = value; }
        }

        public string UserTelephone1
        {
            get { return _userTelephone1; }
            set { _userTelephone1 = value; }
        }

        public string UserTelephone2
        {
            get { return _userTelephone2; }
            set { _userTelephone2 = value; }
        }

        public string UserEmailAddress
        {
            get { return _userEmailAddress; }
            set { _userEmailAddress = value; }
        }

        public string UserAddressLine1
        {
            get { return _userAddressLine1; }
            set { _userAddressLine1 = value; }
        }

        public string UserAddressLine2
        {
            get { return _userAddressLine2; }
            set { _userAddressLine2 = value; }
        }

        public string CompanyName 
        {
            get { return _companyName; }
            set { _companyName = value; }
        }
        public string UserCity
        {
            get { return _userCity; }
            set { _userCity = value; }
        }

        public string UserState
        {
            get { return _userState; }
            set { _userState = value; }
        }

        public string UserZip
        {
            get { return _userZip; }
            set { _userZip = value; }
        }

        public string UserCountry
        {
            get { return _userCountry; }
            set { _userCountry = value; }
        }

        public bool IsDefault
        {
            get { return _isDefault; }
            set { _isDefault = value; }
        }


        #endregion

        private AddressBook(TblAddressBook tblAddressBook)
        {
            this._addressBookId = tblAddressBook.AddressBookId;
            this._userId = tblAddressBook.UserId;
            this._userFirstName = tblAddressBook.UserFirstName;
            this._userLastName = tblAddressBook.UserLastName;
            this._userTelephone1 = tblAddressBook.UserTelephone1;
            this._userTelephone2 = tblAddressBook.UserTelephone2;
            this._userEmailAddress = tblAddressBook.UserEmailAddress;
            this._userAddressLine1 = tblAddressBook.UserAddressLine1;
            this._userAddressLine2 = tblAddressBook.UserAddressLine2;
            this._companyName = tblAddressBook.CompanyName;
            this._userCity = tblAddressBook.UserCity;
            this._userState = tblAddressBook.UserState;
            this._userZip = tblAddressBook.UserZip;
            this._userCountry = tblAddressBook.UserCountry;
            this._isDefault = tblAddressBook.IsDefault;
        }
        public static AddressBook Load(int addressBookId)
        {
            try
            {
                TblAddressBook addressBookData = TblAddressBook.GetTblAddressBookByAddressBookId(addressBookId);

                AddressBook addr = new AddressBook(addressBookData);
                return addr;
            }
            catch (Exception ex)
            {
                return null;
            }

        }

        public void Create()
        {

            try
            {
                this._addressBookId = TblAddressBook.CreateNewTblAddressBook(UserId, UserFirstName, UserLastName, UserTelephone1,UserTelephone2,UserEmailAddress,UserAddressLine1,UserAddressLine2,CompanyName,UserCity,UserState,UserZip,UserCountry,IsDefault);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Update()
        {
            try
            {
                TblAddressBook tblAddressBook = TblAddressBook.GetTblAddressBookByAddressBookId(this._addressBookId);
                tblAddressBook.UserFirstName = this._userFirstName;
                tblAddressBook.UserLastName=this._userLastName;
                tblAddressBook.UserTelephone1=this._userTelephone1;
                tblAddressBook.UserTelephone2=this._userTelephone2;
                tblAddressBook.UserEmailAddress=this._userEmailAddress;
                tblAddressBook.UserAddressLine1=this._userAddressLine1;
                tblAddressBook.UserAddressLine2=this._userAddressLine2;
                tblAddressBook.CompanyName = this._companyName;
                tblAddressBook.UserCity=this._userCity;
                tblAddressBook.UserState=this._userState;
                tblAddressBook.UserZip=this._userZip;
                tblAddressBook.UserCountry=this._userCountry;
                tblAddressBook.IsDefault=this._isDefault;
                return tblAddressBook.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Delete()
        {
            try
            {
                TblAddressBook.DeleteTblAddressBook(this._addressBookId);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static void DeleteAddress(List<int> idList)
        {
            TblAddressBook.DeleteTblAddressBookList(idList);
        }


        public static ReadWriteCustomCollection<AddressBook> GetBackendAddressBookListByCondition(string queryCondition, string sortBy, int firstItem, int pageSize)
        {
            ECommerce.DAL.CustomCollection<TblAddressBook> tblAddressBook = TblAddressBookExtended.GetBackendAddressBookListByCondition(queryCondition, sortBy, firstItem, pageSize);
            return LoadFromCustomCollection(tblAddressBook);
        }

        public static int GetBackendAddressListByConditionCount(string queryCondition)
        {
            int count = TblAddressBookExtended.GetBackendAddressBookListByConditionCount(queryCondition);
            return count;
        }
        public static ReadWriteCustomCollection<AddressBook> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblAddressBook> tblAddressBook)
        {
            ReadWriteCustomCollection<AddressBook> addressBook = new ReadWriteCustomCollection<AddressBook>();
            for (int i = 0; i < tblAddressBook.Count; i++)
            {
                addressBook.Add(new AddressBook(tblAddressBook[i]));
            }
            return addressBook;
        }

        public static ReadWriteCustomCollection<KeyValuePair<string, string>> GetKeyValAddressBook(int loggeduserId)
        {
            ReadWriteCustomCollection<KeyValuePair<string, string>> addressBook = new ReadWriteCustomCollection<KeyValuePair<string, string>>();
            try
            {

               ECommerce.DAL.CustomCollection<TblAddressBook> address = TblAddressBookExtended.GetTblAddressBookByUserId(loggeduserId);
               for (int i = 0; i < address.Count; i++)
               {
                   addressBook.Add(new KeyValuePair<string, string>(address[i].UserFirstName, address[i].AddressBookId.ToString()));
               }
               
            }
            catch (Exception) { }
            return addressBook;
        }
    }
}
